﻿using System;
using System.Data;
using System.Data.SQLite;
using System.Windows.Forms;
using MyFunction.dbFuncion;
using MyFunction.SQLiteHelper;

namespace dataDict
{
    /// <summary>
    /// 设定窗体
    /// </summary>
    public partial class SetUpFrm : Form
    {
        /// <summary>
        /// 数据库联接字串
        /// </summary>
        private string ConStr;
        /// <summary>
        /// 窗体构造函数
        /// </summary>
        public SetUpFrm ( )
        {
            InitializeComponent ( );
        }
        #region 窗口载入事件
        /// <summary>
        ///   窗口载入事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SetUpFrm_Load ( object sender , EventArgs e )
        {
            ConStr = dBFunctions.ConnectionStringSQLite;
        }
        #endregion

        #region 绑定数据库数据
        /// <summary>
        ///  方法：给comboBoxDatabase 绑定数据库数据
        /// </summary>
        public void BindDatabase ( )
        {
            SqliteHelper sqliteHelper = new SqliteHelper ( ConStr );
            string cmdText = "select distinct database from u_systable order by upper(database) ";
            comboBoxDatabase.Items.Clear ( ); //清除原有数据
            if (sqliteHelper.Load ( cmdText ))
            {

                DataTable dt = sqliteHelper.DataSet.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    comboBoxDatabase.Items.Add ( dr[0] );
                }
                if (comboBoxDatabase.Items.Count > 0)
                {
                    comboBoxDatabase.SelectedIndex = 0;
                }
            }
        }
        #endregion

        #region 数据库下拉列表进入事件--数据绑定
        /// <summary>
        ///  comboBoxDatabase 进入事件--数据绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBoxDatabase_Enter ( object sender , EventArgs e )
        {
            BindDatabase ( );
        }
        #endregion

        #region 清除本地数据
        /// <summary>
        ///  方法：初始化，清除本地数据
        /// </summary>
        private void DeleteData ( )
        {
            string database = comboBoxDatabase.Text.Trim ( ).ToUpper();
            DialogResult restult = MessageBox.Show ( "是否清除[" + database + "]所有的数据?" , Text ,
                MessageBoxButtons.YesNo , MessageBoxIcon.Question );
            if (restult == DialogResult.No)
            {
                return;
            }
            
            string cmdText = "delete from u_systable  where upper(database) ='"+database+"'; "+
                "delete from u_syscolcomment where upper(database)='"+database+"';";
            SQLiteConnection Connection = new SQLiteConnection ( ConStr );
            Connection.Open ( );
            SQLiteCommand myCommand = new SQLiteCommand ( cmdText , Connection );
            try
            {
                myCommand.ExecuteNonQuery ( );
                MessageBox.Show ( "数据清理完毕！" );
            }
            catch (Exception ex)
            {
                MessageBox.Show ( ex.Message );
            }
            finally
            {
                Connection.Close ( );
            }
            

        }
        #endregion

        #region
        /// <summary>
        ///  按钮：根据不同tab page 执行相应的动作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripMenuExec_Click ( object sender , EventArgs e )
        {
            int tabpage = tabControl1.SelectedIndex; //当前选中页的序号，从0开始
            switch (tabpage)
            {
                case 0:
                    DeleteData ( );
                    break;
                case 1:
                    break;
                default:
                    break;
            }
        }
        #endregion

        /// <summary>
        ///  菜单：退出窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripMenuExit_Click ( object sender , EventArgs e )
        {
            Close ( );
        }
    }
}
